/* YUIcalendar.js Revision 
 * Code: http://code.google.com/p/yuicalendar/
 * Blog: http://www.YUICoder.com
 * $Revision: 8 $ 
 * $LastChangedDate: 2009-07-17 21:38:13 +0000 (Fri, 17 Jul 2009) $
 * $Author: phpprogrammer1 $
 * Created by Jeffrey Cobb (phpprogrammer1@gmail.com) 2009
 */

function YUIcalendarLoad(){
	var calendarTypes = new Array();
	    calendarTypes['0'] = { className : 'basicCalendar',
			                   calConfig : {navigator : {
							                   strings : {
	    								           month: "Choose Month",
						                           year: "Enter Year",
						                           submit: "OK",
						                           cancel: "Cancel",
						                           invalidYear: "Please enter a valid year"
						                        },
						                        monthFormat: YAHOO.widget.Calendar.LONG,
						                        initialFocus: "year"
						                        }, // end navigator object
						                   close:true
			                	           } // end calConfig object
			                 }; // end the caledarTypes['0'] array item
	
	    calendarTypes['1'] = { className : 'noNavcalendar',
	    		               calConfig : {close : true}
   		                    };
    // loop through the calendarTypes array
	for(varCT = 0; varCT < calendarTypes.length; varCT++){
			    // find all the class names and put them in an array
				var calElements = YAHOO.util.Dom.getElementsByClassName(calendarTypes[varCT].className);
				var element = new Array(); // create empty array to define dynamic variables
				var cal = new Array(); // create empty array to define dynamic variables
				
				// loop through the class names array
				for(i=0;i < calElements.length;i++){
				    element["cal_" + i] = calElements[i]; // defines the input element
					
				    // create a hidden div next to the input element to build the calendar
				    var b = document.createElement('div'); 
					var bottomid = YAHOO.util.Dom.generateId(b,'cal');
					b.id = bottomid;
					YAHOO.util.Dom.setStyle(b , 'display' , 'none' );
					YAHOO.util.Dom.setStyle(b , 'position' , 'absolute' );
					YAHOO.util.Dom.setStyle(b , 'z-index' , '1' );
					YAHOO.util.Dom.insertAfter(b , element["cal_" + i]);
					// create the calendar and render it into the new element just created					 
					cal["cal_" + i]  = new YAHOO.widget.Calendar(cal["cal_" + i],b, calendarTypes[varCT].calConfig );
					cal["cal_" + i].render();
					
					// listen mouse clicks on the input element
					YAHOO.util.Event.addListener(element["cal_" + i], "click", cal["cal_" + i].show, cal["cal_" + i], true);
					
					// detect when a date is selected in the calendar
					cal["cal_" + i].selectEvent.subscribe(function(type, args, obj){
						// get the calendars id
						var calident = obj.id.replace('_t','');
						// get the input elements id
						var sibcal = YAHOO.util.Dom.getPreviousSibling(calident);
						var dates = args[0];
						var date = dates[0];
						var year = date[0], month = date[1], day = date[2];
						// modifier for creating double digit months and days
						if(month <= 9 ){month= '0' + month;}
						if(day <= 9 ){day = '0' + day;}
							// put the selected date into the input element
							sibcal.value = year + "-" + month + "-" + day;	
							// close the calendar
							YAHOO.util.Dom.setStyle(calident,'display','none');
						}, cal["cal_" + i], true);
					
				}
	}		}
YAHOO.util.Event.onDOMReady(YUIcalendarLoad);